home *** CD-ROM | disk | FTP | other *** search
/ PC User 2001 August / APC_Aug2001_CD2.iso / features / web_dev / files / perlbuilderev.exe / %MAINDIR% / Editor Scripts / fixindent2.pl < prev   
Encoding:
Text File  |  2001-06-19  |  3.3 KB  |  113 lines

  1. #Editor Script to fix indentation.
  2. # (c) Solutionsoft, 1998
  3. #     Revised to handle comments correctly and to indent HTML
  4. #     code embedded inside a Perl script.
  5. #     John Weaver, 2000  v1.3 mailto:Perl@jweaver.net
  6.  
  7. #To use:
  8. #Choose Edit | Select ALL then run this editor script.
  9.  
  10. # The current line is in $_.
  11. $reply = $_;
  12. chomp ($reply);              # remove trailing newline
  13. $reply =~ s/^ *//;           # remove leading spaces
  14. $reply =~ s/^\t*//;          # remove leading tabs
  15. if (!$inhtml) {        # if not inside html
  16.   if ($reply =~ /^}.*/) {      # match curly brace at begin of line
  17.     $level--;
  18.   }
  19.   if ($reply =~ /^sub\b/) {      # match sub at begin of line
  20.     $level = 0;
  21.   }
  22.   if ($reply =~ /^else\b/) {      # match else at begin of line
  23.     $level = $prevlevel;
  24.   }
  25.   if ($reply =~ /^elsif\b/) {      # match elsif at begin of line
  26.     $level = $prevlevel;
  27.   }
  28.   if ($reply =~ /^print *<<(.*);/i) {      # match print at begin of line
  29.     $ENDPRINT = $1;
  30.     $PREVLEVEL = $level;
  31.     $inhtml = 1;
  32.   }
  33. } else {           # only if inside html code
  34.   # ---------------------------------------------------------
  35.   my $temp = $ENDPRINT;
  36.   $temp =~ s/^ *//;           # remove leading spaces
  37.   if ($reply =~ /^$temp/)  {
  38.     $level = 0;
  39.     $reply = $ENDPRINT;
  40.     $inhtml = 0;
  41.     $ENDPRINT = '';
  42.   } else {
  43.     if (htmlscan('first', $reply) == -1)  {
  44.       $level--;
  45.     }
  46.   }
  47. }
  48. # ---------------------------------------------------------
  49. $prefix = "" ;
  50. foreach $i (1 .. $level){$prefix = $prefix."  ";}   # concat leading whitespace
  51. #print the result,
  52. #which will replace the selected text in the IDE.
  53. print "$prefix$reply\n";      # output line
  54. $prevlevel = $level;
  55. if (!$inhtml) {        # if not inside html
  56.   if ($PREVLEVEL)   {
  57.     $level = $PREVLEVEL;
  58.     $PREVLEVEL = 0;
  59.   }
  60.   if (!($reply =~ /^\#/)) {       # ignore comment lines
  61.     if ($reply =~ /^.{1,}{ *?$/) {       # find brace at end of line
  62.       $level++;
  63.     }
  64.     if ($reply =~ /^.{1,}{ *\#/) {   # find eol brace followed by comments
  65.       $level++;
  66.     }
  67.     if ($reply =~ /^.{1,}} *?$/) {       # find brace at end of line
  68.       $level--;
  69.     }
  70.     if ($reply =~ /^.{1,}} *\#/) {   # find eol brace followed by comments
  71.       $level--;
  72.     }
  73.   }
  74. } else {
  75.   if (htmlscan('first', $reply) == 1)  {
  76.     $level++;
  77.   }
  78.   if (htmlscan('last', $reply) == -1)  {
  79.     $level--;
  80.   }
  81. }
  82. if ($level < 0) {$level = 0} ;
  83.  
  84. sub htmlscan {
  85.   my ($mode, $line) = @_;
  86.   my @tags = qw(<P <TR <TD <TABLE <BODY
  87.   </P </TR </TD </TABLE </BODY );
  88.   my $found;
  89.   my $tag;
  90.   $mode =~ tr/a-z/A-Z/;      # uppercase mode
  91.   if ($mode eq 'FIRST') {    # first tag in line
  92.     $line =~ /.*?(<.*?)[ >]/;
  93.     $found = $1;
  94.   } else {                   # last tag in line
  95.     $line =~ /.{3,}(<.*)[ >].*$/;
  96.     $found = $1;
  97.   }
  98.   $found =~ tr/a-z/A-Z/;           # uppercase tag found
  99.   if ($found)  {                   # if tag found in line
  100.     foreach $tag (@tags)  {          # scan table
  101.       if ($found eq $tag) {          # is tag in table
  102.         if ($tag =~ /^<\//) {       # does tag have slash
  103.           return -1;       # if end tag
  104.         } else {
  105.           return 1;      # if start tag
  106.         }
  107.       }
  108.     }
  109.   }
  110.   return 0;      # if tag not matched
  111. }
  112.  
  113.